package com.tarena.lbs.business.dao.repository.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tarena.lbs.business.dao.mapper.BusinessMapper;
import com.tarena.lbs.business.dao.mapper.StoreMapper;
import com.tarena.lbs.business.dao.repository.BusinessRepository;
import com.tarena.lbs.business.pojo.po.BusinessPO;
import com.tarena.lbs.business.pojo.po.StorePO;
import com.tarena.lbs.business.pojo.query.BusinessQuery;
import com.tarena.lbs.business.pojo.vo.BusinessStoreVO;
import com.tarena.lbs.business.pojo.vo.BusinessVO;
import com.tarena.lbs.business.pojo.vo.StatisticsVO;
import com.tarena.lbs.business.pojo.vo.StoreVO;
import com.tarena.lbs.content.api.service.ArticleService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Slf4j
@Repository
public class BusinessRepositoryImpl implements BusinessRepository {

    @Autowired
    private BusinessMapper businessMapper;

    @Autowired
    private StoreMapper storeMapper;

    @DubboReference
    private ArticleService articleService;

    @Override
    public Integer save(BusinessPO param) {
        businessMapper.insert(param);
        return param.getId();
    }

    @Override
    public PageResult<BusinessVO> businessInfoList(BusinessQuery query) {
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        List<BusinessVO> businessVOS = businessMapper.selectPageList(query);

        PageInfo<BusinessVO> pageInfo = new PageInfo<>(businessVOS);
        PageResult<BusinessVO> pageResult = new PageResult<>();
        pageResult.setPageNo(pageInfo.getPageNum());
        pageResult.setPageSize(pageInfo.getPageSize());
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setObjects(businessVOS);
        return pageResult;
    }

    @Override
    public BusinessStoreVO businessStoreDetail(Integer businessId) {
        BusinessPO businessPO = businessMapper.selectById(businessId);
//        StorePO storePO = storeMapper.selectById(businessId);
        BusinessStoreVO businessStoreVO = new BusinessStoreVO();
        BeanUtils.copyProperties(businessPO,businessStoreVO);
//        if(storePO!=null){
//            StoreVO storeVO = new StoreVO();
//            BeanUtils.copyProperties(storePO,storeVO);
//        }
        log.info("商家id:{}",businessId);
        List<StoreVO> storeVOList = storeMapper.selectStoreListByBusinessId(businessId);
        log.info("storeVOList:{}",storeVOList);
        businessStoreVO.setStoreVOList(storeVOList);
//        businessMapper.businessStoreDetail(businessId);
        return businessStoreVO;
    }

    @Override
    public void updateBusiness(BusinessPO businessPO) {
        businessMapper.updateById(businessPO);
    }
    @Override
    public BusinessStoreVO getBusiDetail(Integer businessId) {

        BusinessStoreVO businessStoreVO =businessMapper.selectBusinessDetail(businessId);
        return businessStoreVO;
    }

    @Override
    public List<StatisticsVO> selectAllBusiness() {
        return businessMapper.selectAllBusiness();
    }

    @Override
    public int countStoreById(Integer id) {
        return businessMapper.countStoreById(id);
    }

    @Override
    public BusinessStoreVO getBusinessDetail(Integer articleId) {
        BusinessStoreVO businessStoreVO = new BusinessStoreVO();
        com.tarena.lbs.content.pojo.vo.BusinessVO business = articleService.findBusinessById(articleId);
        Integer businessId = business.getId();
        businessStoreVO=businessMapper.selectBusinessDetail(businessId);
        List<StoreVO> storeVOList = storeMapper.selectStoreListByBusinessId(businessId);
        businessStoreVO.setId(business.getId());
        businessStoreVO.setBusinessName(business.getBusinessName());
        businessStoreVO.setStoreVOList(storeVOList);
        return businessStoreVO;
    }
}
